import java.util.ArrayList;

// BankDatabase.java
// Represents the bank account information database 

public class BankDatabase {
	public static ArrayList<Customer> customerList; // array of customers
	private ArrayList<Account> accountList; // array of accounts
	private Admin adm; // creates admin called adm from constructor public Admin

	// no-argument BankDatabase constructor initializes customers
	// new entry in customerList is created for each customer containing
	// FirstName,
	// LastName, CPR, Customertype, Adress, ClientNumber & PIN.
	// each arraylist of customer contains an arraylist of accounts containing
	// accountNumber, available balance, total balance & accountType.
	public BankDatabase() {
		Customer cust = new Customer("Daniel", "Foehns", "090191-1977",
				"Private", "Oesterbro", 2100, 12345, 54321);
		customerList = new ArrayList<Customer>();
		customerList.add(cust);
		cust.createAccount(12345, 100.0, 1000.0, "Overdraft"); // 4 accounts for
																// testing
		cust.createAccount(123456, 110.0, 1100.0, "Savings");
		cust.createAccount(1234567, 120.0, 1200.0, "Budget");
		// 3 accounts pr. customer with 3 different account types/interestRates

		cust = new Customer("Niels-Christian", "Something", "090191-1977",
				"Private", "Vanloese", 2500, 98765, 56789);
		customerList.add(cust);
		cust.createAccount(12345, 100.0, 1500.0, "Overdraft");
		cust.createAccount(123456, 500.0, 1750.0, "Savings");
		cust.createAccount(1234567, 1000.0, 2000.0, "Budget");
		// 3 accounts pr. customer with 3 different account types/interestRates

		cust = new Customer("Kreativbogfoering", "A/S", "12345678", "Business",
				"Frederiksberg", 3242, 11111, 12345);
		customerList.add(cust);
		cust.createAccount(12345, 50.0, 800.0, "Overdraft");
		cust.createAccount(123456, 100.0, 900.0, "Savings");
		cust.createAccount(1234567, 150.0, 1000.0, "Budget");
		// 3 accounts pr. customer with 3 different account types/interestRates

		cust = new Customer("Rasmus", "Pedersen", "123456-7890", "private",
				"Frederiksberg", 1234, 666666, 555555);
		customerList.add(cust);
		cust.createAccount(12345, 5000.0, 80000.0, "Overdraft"); 
		cust.createAccount(123456, 10000.0, 900000.0, "Savings");
		cust.createAccount(1234567, 15000.0, 10000000.0, "Budget");
		// 3 accounts pr. customer with 3 different account types/interestRates

		adm = new Admin(10000, 10000);

	} // end no-argument BankDatabase constructor

	// retrieve Customer object containing specified client number
	public Customer getCustomer(int clientNumber) {
		// loop through customers searching for matching client number
		for (Customer currentCustomer : customerList) {
			// return current customer if match found
			if (currentCustomer.getClientNumber() == clientNumber)
				return currentCustomer;
		} // end for loop

		return null; // if no matching customer was found, return null
	} // end method getCustomer

	// determine whether user-specified account number and PIN match
	// those of an account in the database
	public boolean authenticateUser(int userClientNumber, int userPIN) {
		// attempt to retrieve the customer with the client number
		Customer userCustomer = getCustomer(userClientNumber);

		// if account exists, return result of Customer method validatePIN
		if (userCustomer != null)

		{
			if (userCustomer.validatePIN(userPIN)) {
				updateAccountList(userCustomer.getClientNumber());
				return true;

			} else
				return false;
		} else
			return false; // client number not found, so return false
	} // end method authenticateUser

	public boolean authenticateAdmin(int userAdminID, int userAdminPIN) {
		// attempt to retrieve the admin with the adminID

		// if admin exists, return result of Admin method validatePIN
		if (adm.getAdminID() == userAdminID) {
			if (adm.validatePIN(userAdminPIN)) {
				return true; // sets the value to true

			} else
				return false; // sets the value to false
		} else
			return false; // adminID not found, so return false
	} // end method authenticateAdmin

	public Account getAccount(int accountNumber) {
		// loop through accounts searching for matching account number
		for (Account currentAccount : accountList) {
			// return current account if match found
			if (currentAccount.getAccountNumber() == accountNumber)
				return currentAccount;
		} // end for loop

		return null; // if no matching accounts was found, return null
	} // end method getAccount

	// method for updateAccountList
	public void updateAccountList(int clientNumber) {
		Customer cust = getCustomer(clientNumber);
		accountList = cust.getAccountList();
	} // end method updateAccountList

	// return available balance of Account with specified account number
	public double getAvailableBalance(int userClientNumber) {
		return getAccount(userClientNumber).getAvailableBalance();
	} // end method getAvailableBalance

	// return total balance of Account with specified account number
	public double getTotalBalance(int userAccountNumber) {
		return getAccount(userAccountNumber).getTotalBalance();
	} // end method getTotalBalance

	// return interestBalance of account with specified account number
	public double getInterestBalance(int userAccountNumber) {
		return getAccount(userAccountNumber).getInterestBalance();
	} // end method getInterestBalance

	// return FirstName of client with specified client number
	public String getFirstName(int userClientNumber) {
		return getCustomer(userClientNumber).getFirstName();
	} // end method getFirstName

	// credit an amount to Account with specified account number
	public void credit(int userAccountNumber, double amount) {
		getAccount(userAccountNumber).credit(amount);
	} // end method credit

	// debit an amount from Account with specified account number
	public void debit(int userAccountNumber, double amount) {
		getAccount(userAccountNumber).debit(amount);
	} // end method debit

	// method for new customer (addCustomer)
	public void addCustomer(Customer C) { // retrives the information needed
											// about the new customer
		customerList.add(C);// and
		C.createAccount(12345, 0, 0, "Overdraft");
		C.createAccount(123456, 0, 0, "Savings");
		C.createAccount(1234567, 0, 0, "Budget");
	} // end method addCustomer

	// loops through customerList and returns all found customers.
	// method also corrects for returning all customers as one string
	// so it fits into the "nice" screen concept
	public String viewCustomer() {
		String view = "";
		String x;
		int i = 0;
		for (Customer currentCustomer : customerList) {
			i++;
			x = "| " + i + ". " + currentCustomer.toString();
			int strLen = x.length(); // corrects length according to "nice"
										// screen
			int spaces = 89 - strLen;

			for (int j = 0; j < spaces; j++) {
				x += " ";
			} // end for loop

			x += "|\n";

			view += x;
		} // end for loop

		return view; // returns the list of customers
	}// end method view customer

	// loops through customers to find the customer to remove
	public void removeCustomer(int c) {

		int index = c - 1;
		customerList.remove(index); // takes the clNumber returned
		// from the class ATM and subtracts 1 from the index of our arraylist
		// because an array always starts with index 0.

	}// end method remove customer
} // end class BankDatabase

/**************************************************************************
 * (C) Copyright 1992-2012 by Deitel & Associates, Inc. and * Pearson Education,
 * Inc. All Rights Reserved. * * DISCLAIMER: The authors and publisher of this
 * book have used their * best efforts in preparing the book. These efforts
 * include the * development, research, and testing of the theories and programs
 * * to determine their effectiveness. The authors and publisher make * no
 * warranty of any kind, expressed or implied, with regard to these * programs
 * or to the documentation contained in these books. The authors * and publisher
 * shall not be liable in any event for incidental or * consequential damages in
 * connection with, or arising out of, the * furnishing, performance, or use of
 * these programs. *
 *************************************************************************/
